import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**属性枚举*/
export const enum E_Vip {
	/**等级*/
	LEVEL = 0,
	/**月卡购买时间*/
	MONTH = 1,
	/**大月卡购买时间*/
	BIG_MONTH = 2,
	/**RMB 分*/
	MONEY = 3,
	/**枚举数量*/
	EnumCount = 4
}


/***/
export class Vip {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_VIP_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_VIP_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: Vip[] = [];

	/**创建入口*/
	public static create(json?: string): Vip {
		const c = this.POOL.pop() || new Vip();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**等级 (uint16)*/
	protected _level: number;
	/**月卡购买时间 (long)*/
	protected _month: number;
	/**大月卡购买时间 (long)*/
	protected _bigMonth: number;
	/**RMB 分 (uint32)*/
	protected _money: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (Vip.POOL.indexOf(this) === -1) {
			Vip.POOL.push(this);
		}
	}

	/**等级*/
	public get level() {
		return this._level;
	}


	/**等级*/
	public set level(v: number) {
		this._level = v;
	}


	/**月卡购买时间*/
	public get month() {
		return this._month;
	}


	/**月卡购买时间*/
	public set month(v: number) {
		this._month = v;
	}


	/**大月卡购买时间*/
	public get bigMonth() {
		return this._bigMonth;
	}


	/**大月卡购买时间*/
	public set bigMonth(v: number) {
		this._bigMonth = v;
	}


	/**RMB 分*/
	public get money() {
		return this._money;
	}


	/**RMB 分*/
	public set money(v: number) {
		this._money = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_Vip): any {
		switch (k) {
			case E_Vip.LEVEL: return this._level;
			case E_Vip.MONTH: return this._month;
			case E_Vip.BIG_MONTH: return this._bigMonth;
			case E_Vip.MONEY: return this._money;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_Vip, v: any): void {
		switch (k) {
			case E_Vip.LEVEL: this._level = v; break;
			case E_Vip.MONTH: this._month = v; break;
			case E_Vip.BIG_MONTH: this._bigMonth = v; break;
			case E_Vip.MONEY: this._money = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//等级
		this._level = 0;
		//月卡购买时间
		this._month = 0;
		//大月卡购买时间
		this._bigMonth = 0;
		//RMB 分
		this._money = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._level) {
			json.level = this._level;
		}
		if (this._month) {
			json.month = this._month;
		}
		if (this._bigMonth) {
			json.bigMonth = this._bigMonth;
		}
		if (this._money) {
			json.money = this._money;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.level) {
			this._level = json.level;
		}
		if (json.month) {
			this._month = json.month;
		}
		if (json.bigMonth) {
			this._bigMonth = json.bigMonth;
		}
		if (json.money) {
			this._money = json.money;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeUint16(this._level);
		byte.writeLongNumber(this._month);
		byte.writeLongNumber(this._bigMonth);
		byte.writeUint32(this._money);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._level = byte.readUint16();
		this._month = byte.readLongNumber();
		this._bigMonth = byte.readLongNumber();
		this._money = byte.readUint32();
		return this;
	}


	/**克隆*/
	public clone(): Vip {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = Vip.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = Vip.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//等级
			if (idx === E_Vip.LEVEL) {
				const oldv = this._level;
				this._level = us[i + 1];
				IAsync.dispatch(eventID, idx, this._level, oldv);
				i += 2;
				continue;
			}
			//月卡购买时间
			if (idx === E_Vip.MONTH) {
				const oldv = this._month;
				this._month = us[i + 1];
				IAsync.dispatch(eventID, idx, this._month, oldv);
				i += 2;
				continue;
			}
			//大月卡购买时间
			if (idx === E_Vip.BIG_MONTH) {
				const oldv = this._bigMonth;
				this._bigMonth = us[i + 1];
				IAsync.dispatch(eventID, idx, this._bigMonth, oldv);
				i += 2;
				continue;
			}
			//RMB 分
			if (idx === E_Vip.MONEY) {
				const oldv = this._money;
				this._money = us[i + 1];
				IAsync.dispatch(eventID, idx, this._money, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in Vip with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: Vip): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._level !== other._level) {
			updates.push(E_Vip.LEVEL, this._level);
		}
		if (this._month !== other._month) {
			updates.push(E_Vip.MONTH, this._month);
		}
		if (this._bigMonth !== other._bigMonth) {
			updates.push(E_Vip.BIG_MONTH, this._bigMonth);
		}
		if (this._money !== other._money) {
			updates.push(E_Vip.MONEY, this._money);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(Vip.TOTAL_EVENT_ID, this.getV(0));
	}

}
